package com.qxsx.midi.activity.exam;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.CircleImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by LYJ on 2015/9/16.
 * 评价老师
 */
public class AssessTeachertActivity extends BaseActivity implements OnClickListener {
    private CustomProgress customProgress;

    private Button btn_commit;
    String id, exam_id, midi_id, head_img;
    private EditText tv_teacher_desc;
    CircleImageView image_icon;
    private TextView tv_teacher_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam_assess);
        setTitle("评价老师");
        setNavBtn(R.mipmap.back, 0);
        findById();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");//得到ID
        exam_id = intent.getStringExtra("exam_id");
        xinxi();
    }

    private void findById() {
        btn_commit = (Button) findViewById(R.id.btn_commit);
        btn_commit.setOnClickListener(this);
        tv_teacher_desc = (EditText) findViewById(R.id.tv_teacher_desc);
        image_icon = (CircleImageView) findViewById(R.id.image_icon);
        tv_teacher_id = (TextView) findViewById(R.id.tv_teacher_id);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }

    private void xinxi() {
        customProgress = CustomProgress.show(AssessTeachertActivity.this, "正在加载中....", true, null);
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("user_id", id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("109", array, new callBackJsonArray() {
                    @Override
                    public void run(Object code, Object message, JSONArray array) {
                        //  Log.e("-------------", code.toString());
                        if (!code.toString().equals("200")) {
                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                        } else if (array == null) {
                            Log.w("array", array + "");
                            return;
                        } else {
                            try {
                                JSONObject jsonObject = array.getJSONObject(0);
                                midi_id = jsonObject.getString("midi_id");

                                head_img = jsonObject.getString("head_img");

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            tv_teacher_id.setText(midi_id);
                            if (!head_img.equals("") && head_img != null && !head_img.isEmpty()) {

                                MyNet.setNetImage(AppContext.getContext(), image_icon, head_img);

                            }
                        }
                        customProgress.dismiss();
                    }
                }

        );


    }


    private void assesteacher() {
        customProgress = CustomProgress.show(AssessTeachertActivity.this, "正在提交，请稍后....", true, null);

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        try {
            object.put("exam_id", exam_id);
            object.put("user_id", user_id);
            object.put("teacher_id", id);
            object.put("give_content", tv_teacher_desc.getText().toString().trim());
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("8013", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                ShowToast(message.toString());
                finish();

                customProgress.dismiss();
            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_commit:
                //点击编辑
                if (TextUtils.isEmpty(tv_teacher_desc.getText().toString().trim())) {
                    ShowToast("请输入您的评论");
                } else {
                    assesteacher();
                }

                break;
        }

    }
}
